package top.shiyiri.reflection;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Aunean
 * @date 2022/1/12
 */
/*
了解类的加载器
 */
public class ClassLoaderTest {

    @Test
    public void test1() {
        //对于自定义类，使用系统类加载器进行加载
        ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
        System.out.println(classLoader);

        //调用系统类加载器的getParent()：获取扩展类加载器
        ClassLoader classLoader1 = classLoader.getParent();
        System.out.println(classLoader1);

        //调用扩展类加载器的getParent()：无法获取引导类加载器
        //引导类加载器主要负责Java的核心类库，无法加载自定义类
        ClassLoader classLoader2 = classLoader1.getParent();
        System.out.println(classLoader2);
    }

    /*
    Properties：用来读取配置文件
     */
    @Test
    public void test2() throws Exception {
        Properties properties = new Properties();
        //方式一
        FileInputStream fis = new FileInputStream("jdbc.properties");
        properties.load(fis);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        System.out.println("user：" + user + "，password：" + password);
        fis.close();
    }
    @Test
    public void test3() throws Exception {

        Properties properties = new Properties();
        //方式二：
        ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream("D:\\shangguiguProject\\JavaProject\\chapter15_reflection\\jdbc.properties");
        properties.load(is);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        System.out.println("user：" + user + "，password：" + password);
    }
}
